﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BOHVote.BLL;
using BOHVote.Entity;
 

namespace BOH.Interview
{
    public partial class interviewlist : System.Web.UI.Page
    {
        private BHUser user;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["User"] != null)
                {
                    user = (BHUser)Session["User"];
                    switch (user.RightType)
                    {
                        case "S":
                            LoadDrop(user.RightCode);
                            this.drpApplyStyle.Enabled = false;
                            SpecialityBind(user.RightCode);
                            break;
                        case "C":
                        case "A":
                        default:
                            LoadDrop("");
                            this.drpApplyStyle.Enabled = true;
                            break;
                    }
                }
                
            }
        }

        #region <<  绑定DropdownList  >>


        public void LoadDrop(string subjectID)
        {
            SubjectBindID(subjectID);
            BindApplyStyle();
            getJobRank();
            //BindPeriod();
        }

        public void getJobRank()
        {
            drpQuestionLevel.Items.Clear();
            ListItem li1 = new ListItem("请选择级别", "-1");
            this.drpQuestionLevel.Items.Add(li1);
            DataSet dst = OtherClass.getJobRank();
            foreach (DataRow dr in dst.Tables[0].Rows)
            {
                ListItem li = new ListItem(dr["JobRankName"].ToString(), dr["JobRankLevel"].ToString());
                drpQuestionLevel.Items.Add(li);
            }
        }

        public void SubjectBindID(string SubjectID)
        {
            this.drpSubject.Items.Clear();
            ListItem li1 = new ListItem("请选择学科组", "-1");
            this.drpSubject.Items.Add(li1);
            if (String.IsNullOrEmpty(SubjectID))
            {
                DataSet dst = Subject.getAllASubject();

                foreach (DataRow dr in dst.Tables[0].Rows)
                {
                    if (dr["SubjectID"].ToString().Length > 0)
                    {
                        ListItem li = new ListItem(dr["SubjectName"].ToString(), dr["SubjectID"].ToString());
                        this.drpSubject.Items.Add(li);
                    }
                }
            }
            else
            {
                string SubjectName = Subject.getSubjectNameBySubjectID(SubjectID);
                ListItem li = new ListItem(SubjectName, SubjectID);
                this.drpSubject.Items.Add(li);
            }
        }

        public void SubjectBind(string level)
        {
            this.drpSubject.Items.Clear();
            DataSet dst;
            if (String.IsNullOrEmpty(level))
                dst = Subject.getAllASubject();
            else
                dst = Subject.getSubjectByLevel(level);
            //dst = Subject.
            ListItem li1 = new ListItem("请选择学科组", "-1");
            this.drpSubject.Items.Add(li1);
            foreach (DataRow dr in dst.Tables[0].Rows)
            {
                if (dr["SubjectID"].ToString().Length > 0)
                {
                    ListItem li = new ListItem(dr["SubjectName"].ToString(), dr["SubjectID"].ToString());
                    this.drpSubject.Items.Add(li);
                }
            }
        }

        protected void drpSubject_SelectedIndexChanged(object sender, EventArgs e)
        {
            string strSubjectID = ((DropDownList)sender).SelectedValue;
            if (strSubjectID != "-1")
            {
                SpecialityBind(strSubjectID);
            }
            else
            {
                drpSpeciality.Items.Clear();
                ListItem li1 = new ListItem("请选择专业", "-1");
                this.drpSpeciality.Items.Add(li1);
            }
        }

        public void SpecialityBind(string SubjectID)
        {
            drpSpeciality.Items.Clear();
            ListItem li1 = new ListItem("请选择专业", "-1");
            this.drpSpeciality.Items.Add(li1);

            DataSet dst = Subject.getAllSpecialityBySubjectID(SubjectID);
            foreach (DataRow dr in dst.Tables[0].Rows)
            {
                ListItem li = new ListItem(dr["SpecialityName"].ToString(), dr["SpecialityID"].ToString());
                this.drpSpeciality.Items.Add(li);
            }
        }

        public void BindApplyStyle()
        {
            DataSet dst = OtherClass.dstGetApplyStyle();
            BindDropDownList(drpApplyStyle, dst);
        }

        public void BindDropDownList(ListControl Controls, DataSet dst)
        {
            Controls.Items.Clear();
            ListItem list = new ListItem("请选择", "-1");
            Controls.Items.Add(list);
            foreach (DataRow dr in dst.Tables[0].Rows)
            {
                ListItem itemlist = new ListItem(dr["dict_Display"].ToString(), dr["dict_Value"].ToString());
                Controls.Items.Add(itemlist);
            }
        }
        protected void drpApplyStyle_SelectedIndexChanged(object sender, EventArgs e)
        {
            string level = drpApplyStyle.SelectedValue;
            SubjectBind(level);
            drpSpeciality.Items.Clear();
            ListItem li1 = new ListItem("请选择专业", "-1");
            this.drpSpeciality.Items.Add(li1);
        }

        #endregion

        #region <<  binding GridView  >>

        protected void AspNetPager1_PageChanged(object src, EventArgs e)
        {
            QuestionDataBind();
        }

        public void QuestionDataBind()
        {
            if(drpSubject.SelectedValue.Trim() != "-1")
            {
                int strQuestionLevel = -1;
                string strSubject = string.Empty;
                string strSpeciality = string.Empty;

                if (drpQuestionLevel.SelectedValue.Trim().Length != 0)
                    strQuestionLevel = Convert.ToInt16(drpQuestionLevel.SelectedValue);
                if (drpSubject.SelectedValue.Trim() != "-1")
                    strSubject = drpSubject.SelectedValue.Trim();
                if (drpSpeciality.SelectedValue.Trim() != "-1")
                    strSpeciality = drpSpeciality.SelectedValue.Trim();
                DataSet dst = InterviewClass.getQuestions(string.Empty, strSubject, strSpeciality, "A", (string)Session["ApplyPeriod"]);
                QuestionBind(dst);
            }
        }

        public void QuestionBind(DataSet dst)
        {
            if (dst != null && dst.Tables[0].Rows.Count > 0)
            {
                AspNetPager1.RecordCount = dst.Tables[0].Rows.Count;
                if (AspNetPager1.RecordCount != 0)
                {
                    labNoRecord.Visible = false;
                    AspNetPager1.Visible = true;
                    PagedDataSource pds = new PagedDataSource();
                    pds.DataSource = dst.Tables[0].DefaultView;
                    pds.AllowPaging = true;
                    pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
                    pds.PageSize = AspNetPager1.PageSize;
                    InterviewQuestionsList.DataSource = pds;
                    InterviewQuestionsList.DataBind();
                }
                else
                {
                    InterviewQuestionsList.DataSource = null;
                    InterviewQuestionsList.DataBind();
                    AspNetPager1.Visible = false;
                    labNoRecord.Visible = true;
                }
            }
            else
            {
                AspNetPager1.Visible = false;
                labNoRecord.Visible = true;
                InterviewQuestionsList.DataSource = null;
                InterviewQuestionsList.DataBind();
            }
        }

        protected void butSearch_Click(object sender, EventArgs e)
        {
            QuestionDataBind();
        }

        #endregion

        protected void InterviewQuestionsList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            GridViewRow dr = this.InterviewQuestionsList.Rows[e.RowIndex];
            string QID = ((HiddenField)dr.FindControl("hidQID")).Value;
            InterviewClass.DelQuestionByID(QID);

            QuestionDataBind();
        }

    }
}
